<html>
<head><meta charset="utf-8"><title>c variadics in MIR · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html">c variadics in MIR</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="152299732"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152299732" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152299732">(Dec 21 2018 at 00:57)</a>:</h4>
<p>I'm working on implementing C variadics in Rust and I'm having a hard time figuring out the best way to generate the correct MIR</p>



<a name="152299795"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152299795" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152299795">(Dec 21 2018 at 00:58)</a>:</h4>
<p>I currently see three approaches</p>



<a name="152299877"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152299877" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152299877">(Dec 21 2018 at 01:00)</a>:</h4>
<p>1) create a new <code>TyKind</code> for C variadic arguments. This gets a bit hard in <code>typeck</code> method resolution because we need to adjust it to be the type of the <code>va_list</code> language item</p>



<a name="152299988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152299988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152299988">(Dec 21 2018 at 01:03)</a>:</h4>
<p>2) Change <code>FnType</code>s <code>variadic</code> boolean to the name of the user provided argument and when generating the function signature remove the last argument. (I need the name to generate <code>va_start</code>/<code>va_end</code> correctly)</p>



<a name="152300069"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152300069" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152300069">(Dec 21 2018 at 01:04)</a>:</h4>
<p>3) Rely on <code>FnSig</code>s <code>variadic</code> boolean and somehow hack stuff so that the input signature is something like <code>fn (i32, VaList, ...)</code> but in codegen we generate <code>fn (i32, ...)</code>.</p>



<a name="152300111"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152300111" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152300111">(Dec 21 2018 at 01:05)</a>:</h4>
<p>Suggestions on code and/or documentation to read would be appreciated!</p>



<a name="152300137"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152300137" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152300137">(Dec 21 2018 at 01:05)</a>:</h4>
<p>I'm currently just trying things and spending some quality time in <code>gdb</code> <span class="emoji emoji-1f603" title="smiley">:smiley:</span></p>



<a name="152300193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152300193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152300193">(Dec 21 2018 at 01:06)</a>:</h4>
<p>For reference the RFC is here <a href="https://github.com/rust-lang/rfcs/blob/master/text/2137-variadic.md" target="_blank" title="https://github.com/rust-lang/rfcs/blob/master/text/2137-variadic.md">https://github.com/rust-lang/rfcs/blob/master/text/2137-variadic.md</a></p>



<a name="152319792"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152319792" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152319792">(Dec 21 2018 at 09:55)</a>:</h4>
<p>to 1) I think it would be best not to have a custom <code>TyKind</code>, but instead wherever you are currently producing your <code>TyKind::Variadic</code> value, obtain the <code>va_list</code> lang item's <code>DefId</code> via <code>tcx.language_items().va_list</code> and yield a <code>TyKind::Adt</code> with the given <code>DefId</code> and empty substs</p>



<a name="152328888"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152328888" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152328888">(Dec 21 2018 at 13:04)</a>:</h4>
<p>This is what my current attempts at 2 and 3 do, but then we generate the function signature <code>fn (i32, VaList, ...)</code>.</p>



<a name="152329064"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152329064" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152329064">(Dec 21 2018 at 13:07)</a>:</h4>
<p>So then when we generate the MIR I guess I need to remove the <code>VaList</code> from the argument list after it is added to the <code>local_decls</code>, but remove it from the function input?</p>



<a name="152330577"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152330577" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152330577">(Dec 21 2018 at 13:33)</a>:</h4>
<p>Can you show me the code where you generate new function signatures?</p>



<a name="152330645"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152330645" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152330645">(Dec 21 2018 at 13:34)</a>:</h4>
<p>What's your goal in this situation? have the function signature be <code>fn(i32, VaList)</code>?</p>



<a name="152330702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152330702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152330702">(Dec 21 2018 at 13:35)</a>:</h4>
<p>ah, so you want typeck to treat it as <code>fn(i32, VaList)</code> and mir generation to treat it as <code>fn(i32, ...)</code>?</p>



<a name="152330908"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152330908" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152330908">(Dec 21 2018 at 13:38)</a>:</h4>
<p>hmm... reading the RFC suggests to me that you would want the <code>VaList</code> argument in typeck and MIR generation and everything, and only make codegen forget about that argument</p>



<a name="152330924"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152330924" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152330924">(Dec 21 2018 at 13:38)</a>:</h4>
<p>or am I misinterpreting something here?</p>



<a name="152332472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152332472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152332472">(Dec 21 2018 at 14:06)</a>:</h4>
<blockquote>
<p>ah, so you want typeck to treat it as fn(i32, VaList) and mir generation to treat it as fn(i32, ...)?</p>
</blockquote>



<a name="152332475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152332475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152332475">(Dec 21 2018 at 14:06)</a>:</h4>
<p>exactly</p>



<a name="152332650"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152332650" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152332650">(Dec 21 2018 at 14:10)</a>:</h4>
<blockquote>
<p>you would want the VaList argument in typeck and MIR generation and everything, and only make codegen forget about that argument</p>
</blockquote>
<p>I think that could work...</p>



<a name="152332662"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152332662" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152332662">(Dec 21 2018 at 14:10)</a>:</h4>
<p>I might need to add a boolean on <code>ArgType</code> to indicate to <code>codegen</code> that the arg is indeed a variadic argument</p>



<a name="152332746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152332746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152332746">(Dec 21 2018 at 14:12)</a>:</h4>
<blockquote>
<p>or am I misinterpreting something here?</p>
</blockquote>
<p>nope</p>



<a name="152339459"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152339459" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152339459">(Dec 21 2018 at 15:57)</a>:</h4>
<blockquote>
<p>I might need to add a boolean on <code>ArgType</code> to indicate to <code>codegen</code> that the arg is indeed a variadic argument</p>
</blockquote>
<p>it could also be a new variant in the <code>PassMode</code> enum</p>



<a name="152339460"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152339460" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152339460">(Dec 21 2018 at 15:57)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/blob/059e6a6f57f4e80d527a3cd8a8afe7f51f01af8e/src/librustc_target/abi/call/mod.rs#L37" target="_blank" title="https://github.com/rust-lang/rust/blob/059e6a6f57f4e80d527a3cd8a8afe7f51f01af8e/src/librustc_target/abi/call/mod.rs#L37">https://github.com/rust-lang/rust/blob/059e6a6f57f4e80d527a3cd8a8afe7f51f01af8e/src/librustc_target/abi/call/mod.rs#L37</a></p>



<a name="152339536"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152339536" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152339536">(Dec 21 2018 at 15:58)</a>:</h4>
<p>Ah nice!</p>



<a name="152339543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152339543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152339543">(Dec 21 2018 at 15:59)</a>:</h4>
<p>Thanks I'll check that out</p>



<a name="152339567"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152339567" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152339567">(Dec 21 2018 at 15:59)</a>:</h4>
<p>I haven't looked into PassMode yet, so I'll have to read the code</p>



<a name="152341762"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152341762" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152341762">(Dec 21 2018 at 16:35)</a>:</h4>
<p>FWIW my current progress is here <a href="https://github.com/dlrobertson/rust/tree/varargs0" target="_blank" title="https://github.com/dlrobertson/rust/tree/varargs0">https://github.com/dlrobertson/rust/tree/varargs0</a></p>



<a name="152344312"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152344312" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152344312">(Dec 21 2018 at 17:21)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@Oli</span> After looking at <code>PassMode</code>, it definitely seems like the right direction. Thanks</p>



<a name="152344325"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/c%20variadics%20in%20MIR/near/152344325" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dlrobertson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/c.20variadics.20in.20MIR.html#152344325">(Dec 21 2018 at 17:21)</a>:</h4>
<p>In retrospect it seems so obvious to look at what is done for zst's <span class="emoji emoji-1f603" title="smiley">:smiley:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>